import logging
import os
import time

from selenium.webdriver.support.wait import WebDriverWait

from tools.config import DIR_PATH
from tools.logger import logger
from utils import UtilsDriver

#创建的基类
#创建对象库的基类


# 创建的基类
# 创建对象库的基类
class BasePage:
    def __init__(self,driver):
        self.driver = driver
        self.logger=logger
        logger.info("正在初始化对象")

    def get_element(self, location):
        wait = WebDriverWait(self.driver, 10, 1)
        element = wait.until(lambda x: x.find_element(*location))
        return element

    def base_find(self, loc, timeout=10, poll=0.5):
        logger.info("正在查找元素:{}".format(loc))
        return WebDriverWait(self.driver, timeout, poll).until(lambda x: x.find_element(*loc))

    def base_input(self, loc, value):
        logger.info("正在查找元素：{}".format(loc))
        self.base_find(loc).clear()
        self.base_find(loc).send_keys(value)

    def base_get_text(self, loc):
        logger.info("正在查找元素：{}".format(loc))
        return self.base_find(loc).text()

    def base_click(self, loc):
        logger.info("正在调用元素：{}".format(loc))
        self.base_find(loc).click()

    def base_default_frame(self):
        self.driver.switch_to.default_content()

    def base_switch_frame(self, loc):
        logger.info("正在调用切换frame方法，切换对象:}".format(loc))  # 获取元素
        el = self.base_find(loc)
        # 执行切换
        self.driver.switchto.frame(el)

    def base_get_img(self):
        logger.info("正调用截图方法")
        img_path = (DIR_PATH + os.sep + "img" + os.sep + "{}.png".format(time.strftime("%Y%m%d%H%M%S")))
        self.driver.get_screenshot_as_file(img_path)


